//
//  ResponseModel.swift
//  Health-Doctor
//
//  Created by xiesiyu on 2022/3/3.
//  Copyright © 2022 掌上卫健. All rights reserved.
//

import HandyJSON

enum CodeType: Int {
    case success = 200
    
    case error04 = 4
    case error400 = 400
    case error404 = 404
    case error415 = 415
    case error502 = 502
    case errorOther = 100000
    //无网络连接
    case noNetworkConnection = -1020
}


class ResponseModel: HandyJSON {
    
    /// 返回码（数据业务状态码）
    var status = 0
    //扩展状态码
    var extendCode = 0
    //扩展数据
    var extendData:Any?
    ///处理编码(网络请求状态码)
    var httpStatus:CodeType?
    /// 信息
    var msg = ""
    /// 分页参数-当前页条数
    var pageSize:Int?
    /// 分页参数-当前页
    var pageNo:Int?
    /// 分页参数-数据总统条数，用于计算最大分页数
    var total:Int?
    /// 满足条件的数据条数
    var dataCount:Int?
    /// 未同步条数
    var unsyncCount:Int?
    var time = ""
    
    ///原始数据(通过方法获取)
    private var data:Any?
    
    ///是否请求成功
    func isSuccess()->Bool {
        return .success == httpStatus && status == 0
    }
    
    //获取成功的模型
    public class func getSuccessModel(data:Any?)->ResponseModel{
        let model = ResponseModel()
        model.httpStatus = .success
        model.status = 0
        model.setDataValue(data)
        return model
    }
    
    public func getOriginalData()->Any?{
        return data
    }
    
    //设置data的数据，为了后期数据拦截扩展，不要用属性的方式赋值
    func setDataValue(_ jsonDic:Any?){
        self.data = jsonDic
    }
    ///把data转 字典
    func dicData()->[String:Any] {
        if let dic = data as? [String:Any] {
            if let tempData = dic["data"] as? [String:Any]{
                return tempData
            }
        }
        return [:]
    }
   
    
    ///获取data列表数据
    func getListData()->[[String:Any]] {
        if let dic = data as? [String:Any] {
            if let tempData = dic["data"] as? [[String:Any]] {
                return tempData
            }
        }
        return []
    }
    
    //data是个字符串
    func getStringData()->String {
        if let dic = data as? [String:Any] {
            let string = dic.strValue("data")
            return string
        }
        return ""
    }
    
    
    ///获取data
    func getDataValue()->Any? {
        return data
    }
    
    ///extendData
    func getExtendDataStrValue()->String{
        if let val = self.extendData{
            return "\(val)"
        }
        return ""
    }
    //转字典
    func getExtendDicVal()->[String:Any]{
        if let dic = self.extendData as? [String:Any] {
            return dic
        }
        //是个json
        let json = getExtendDataStrValue()
        if json.isEmpty{
            return [:]
        }
        return json.toDic()
    }
    
    
    required init(){}
}



enum HttpCode: Int {
    case OK = 0
    case OLDVERSION = 1
    case UNEXPECTED_CLIENT = 2
    case FAILED_PARAM = 3
    case UNEXIST = 4
    case OVERDUE = 5
    case DENIED = 6
    case UNAUTHENTICATED = 7
    case LOCKED_STATE = 8
    case ERROR = 9
    case FAILURE = 10
    case DUPLICATE_DATA = 11
    case FAILED_VCODE = 12
    case FREQUENT_REQUEST = 13
    case OVERDUE_VCODE = 14
    case VALID_ERROR = 15
    case DATA_TOOLONG = 16
    case DATA_TEYP_DENIED = 17
    case FAILED_VALIDATE_IDENTITY = 18
    case NOT_WORKED = 19
    case SPKG_REPEAT = 20
    case PASSWORD_ERROR = 101
    case LOGIN_ANOTHER = 102
    case PHONE_BOUND = 103
    case ALREADY_CERTIFICATION = 104
    case SPKG_INVALID = 105
    case IN_AUUDIT = 106
    case DEV_ACTIVATED = 107
    case IDCARD_USERD = 108
    case DEV_NOT_ACTIVATED = 109
    case ERROR_STATUS = 110
    case TAG_Full = 200
    case OLD_ERROR_99 = 99
    
    var tipString:String {
        switch self {
        case .OK : return "OK"
        case .OLDVERSION : return "版本过期"
        case .UNEXPECTED_CLIENT : return "用户角色和对应的app端不一致"
        case .FAILED_PARAM : return "请求参数错误"
        case .UNEXIST : return "访问的数据不存在"
        case .OVERDUE : return "数据过期"
        case .DENIED : return "权限不足造成的拒绝访问"
        case .UNAUTHENTICATED : return "您还没有通过实名认证哟"
        case .LOCKED_STATE : return "状态被锁定,异常状态"
        case .ERROR :return "系统运行错误"
        case .FAILURE : return "操作失败"
        case .DUPLICATE_DATA : return "数据已经在系统存在了"
        case .FAILED_VCODE : return "验证码错误"
        case .FREQUENT_REQUEST : return "操作太过频繁"
        case .OVERDUE_VCODE : return "验证码已过期"
        case .VALID_ERROR : return "token 验证失败"
        case .DATA_TOOLONG : return "数据太长"
        case .DATA_TEYP_DENIED : return "数据类型不对"
        case .FAILED_VALIDATE_IDENTITY : return "身份验证错误"
        case .NOT_WORKED : return "您还没有通过职业认证哟"
        case .SPKG_REPEAT : return "基础家庭医生服务包只能签约一个"
        case .PASSWORD_ERROR : return "账号或密码不正确"
        case .LOGIN_ANOTHER : return "您已经在其他设备登录了"
        case .PHONE_BOUND : return "该手机号已经绑定"
        case .ALREADY_CERTIFICATION : return "您已经认证过了"
        case .SPKG_INVALID : return "服务包无效"
        case .IN_AUUDIT : return "认证中"
        case .DEV_ACTIVATED : return "设备已经激活了"
        case .IDCARD_USERD : return "身份证已经被使用"
        case .DEV_NOT_ACTIVATED : return "设备还未激活了"
        case .ERROR_STATUS : return "状态不正确"
        case .TAG_Full : return "居民标签数量已经满了"
        case .OLD_ERROR_99:return ""
        }
    }
}
